package com.sgb.kjwl.viewmodel.pick;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.PickBidListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickBidListFragModel extends BaseViewModel {

    private PickProjectRepertory repertory;
    private PickBidListFragment fragment;
    private int page = 1;
    private int size = 10;
    public int total;

    public PickBidListFragModel(PickBidListFragment fragment, PickProjectRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getBiddingList(String companyName,boolean b) {
        Map map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", 10);
        map.put("companyName", companyName);
        map.put("page", page);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getBiddingList(map).enqueue(new Callback<BaseEntity<ListResponse<List<BiddingEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<BiddingEntity>>>> call, Response<BaseEntity<ListResponse<List<BiddingEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!b) {
                        fragment.showList(response.body().getData().getList(), response.body().getData().isLastPage());
                    } else {
                        fragment.lodMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    }

                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<BiddingEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUrlSetting(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
